package com.hn.vue.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.vue.mapper.MenuMapper;
import com.hn.vue.mapper.RoleMapper;
import com.hn.vue.mapper.RoleMenuMapper;
import com.hn.vue.param.RoleParam;
import com.hn.vue.pojo.*;
import com.hn.vue.service.RoleService;
import com.hn.vue.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * hanni
 * 2019/3/1 0001
 * 15:51
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;


    @Override
    public List<Role> findRoleAll() {
        log.info("查询所有的角色");
//        RoleExample example = new RoleExample();
//        example.createCriteria().andDelFlagEqualTo((byte)0);
        return roleMapper.selectByExample(null);
    }

    @Override
    public PageVo<Role> findRoleList(RoleParam param) {
        PageHelper.startPage(param.getCPage(),param.getPageSize());
        List<Role> rList = roleMapper.findByParams(param);
        PageInfo<Role> pageInfo = new PageInfo<>(rList);
        PageVo pageVo = new PageVo(pageInfo.getTotal(),pageInfo.getList());


        return pageVo;
    }

    @Override
    public void saveRole(Role role) {
        if(role.getId()==null){
            role.setDelFlag((byte)0);
            role.setCreateTime(new Date());
            role.setCreateBy("admin");
            roleMapper.insert(role);
        }else{
            role.setCreateTime(new Date());
            roleMapper.updateByPrimaryKeySelective(role);
        }
    }

    @Override
    public void updateDeleteFlag(String idd) {
        String[] split = idd.split(",");
        Role role = new Role();

        for (String s : split) {
            role.setId(Long.parseLong(s));
            role.setDelFlag((byte)1);
            roleMapper.updateByPrimaryKeySelective(role);
        }
    }

    @Override
    public List<Long> selectMenuIdsByRoleId(Long roleId) {

        RoleMenuExample roleMenuExample = new RoleMenuExample();
        roleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(roleMenuExample);
        List<Long> ids = new ArrayList<>();
        for (RoleMenu roleMenu : roleMenus) {
            ids.add(roleMenu.getMenuId());
        }
        roleMenuMapper.selectByExample(roleMenuExample).stream().forEach(x->ids.add(x.getMenuId()));
        return ids;
    }

    @Override
    public void updateRoleMenu(Long roleId, String menuIds) {
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        roleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(roleMenuExample);
        if (menuIds !=null &&!"".equals(menuIds)){
            String[] split = menuIds.split(",");
            for (String menuid : split) {
                RoleMenu rm = new RoleMenu();
                rm.setRoleId(roleId);
                rm.setCreateTime(new Date());
                rm.setMenuId(Long.parseLong(menuid));
                roleMenuMapper.insert(rm);
            }
        }

    }
}
